package godis

import (
	"regexp"
	"time"
)

type CachePolicy interface {
	Before(key string)
	IfNull(key string, value interface{})
	SetOperation(opt *StringOperation)
}

//
//CrossPolicy 检查key 正则
type CrossPolicy struct {
	KeyRegx string
	Expire  time.Duration
	Opt     *StringOperation
}

func NewCrossPolicy(keyRegx string, expire time.Duration) *CrossPolicy {
	return &CrossPolicy{KeyRegx: keyRegx, Expire: expire}
}

func (self *CrossPolicy) Before(key string) {

	if !regexp.MustCompile(self.KeyRegx).MatchString(key) {
		panic("error cache key")
	}

}

func (self *CrossPolicy) IfNull(key string, value interface{}) {

	self.Opt.Set(key, value, WithExpire(self.Expire))
}

func (self *CrossPolicy) SetOperation(opt *StringOperation) {
	self.Opt = opt
}
